<?php
 session_start();
require_once("bdd.php");

if( isset($_POST['texte']) && isset($_POST['note']) && isset($_SESSION['utilisateur']) && isset($_POST['idpicto']) && isset($_POST['notepicto']) && isset($_POST['nb']) ) {

$text = addslashes( $_POST['texte'] );
$notepicto = $_POST['notepicto'];
 
$date =  date('Y-m-d H:i:s');

$connexion->exec("INSERT INTO commentaire (texte, iduser, idpicto, date, note)
					VALUES( '".$text."', '".$_SESSION['utilisateur']."', '".$_POST['idpicto']."', '".$date."', '".$_POST['note']."')");

					$tmp = $_POST['nb']+1;

	$notepicto = ($notepicto*$_POST['nb'] + $_POST['note'])/$tmp;
					
$connexion->exec("UPDATE picto SET note='".$notepicto."' WHERE idpicto=".$_POST['idpicto']."");
$connexion->exec("UPDATE picto SET nb='".$tmp."' WHERE idpicto=".$_POST['idpicto']."");
			
					 $hostAccueil  = $_SERVER['HTTP_HOST'];
					 $url = '/pictogramme.php?id='.$_POST['idpicto'];
					header("Location: http://$hostAccueil$url");
}
else {
	 $hostAccueil  = $_SERVER['HTTP_HOST'];
 header("Location: http://$hostAccueil");
}

?>